package org.southyin.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect // 定义切面类
public class MyAspectConfig {

//    @Before("org.southyin.aop.MyPointcut.a()") // 前置通知
    public void beforeAdvice(){
        System.out.println("开启事务");
    }

//    @After("org.southyin.aop.MyPointcut.a()") // 后置通知 : 切点方法无论是否由异常都会执行
    public void afterAdvice(){
        System.out.println("结束事务");
    }

//    @AfterThrowing(value = "org.southyin.aop.MyPointcut.a()",throwing = "e") // 异常通知 ：只有切点方法有异常，才会执行
    public void afterAdvice2(Exception e) {
        System.out.println(e.getMessage());
    }

//    @AfterReturning("org.southyin.aop.MyPointcut.a()") // 返回通知 : 只有切点方法无异常，才会执行
    public void afterAdvice3() {
        System.out.println("AfterReturning");
    }

    //    @Around("org.southyin.aop.MyPointcut.a()")// 环绕通知
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕-前");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕-后");
        return proceed;
    }
}
